/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.org.desa;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author 00778898431
 */
public class Conversor extends MIDlet implements CommandListener {
    
    private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Command exitCommand;
    private Command okCommand;
    private Form form;
    private TextField prescmiligramas;
    private TextField medMililitros;
    private Spacer spacer;
    private Spacer spacer1;
    private ImageItem imageLogo;
    private StringItem labelResultado;
    private Spacer spacer2;
    private StringItem labelPrescricao;
    private TextField medMiligramas;
    private StringItem labelMedicamento;
    private Spacer spacer3;
    private Image image;
//</editor-fold>//GEN-END:|fields|0|

    /**
     * The HelloMIDlet constructor.
     */
    public Conversor() {
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getForm());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display; if
     * <code>null</code>, then
     * <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == form) {//GEN-BEGIN:|7-commandAction|1|19-preAction
            if (command == exitCommand) {//GEN-END:|7-commandAction|1|19-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|2|19-postAction
                // write post-action user code here
            } else if (command == okCommand) {//GEN-LINE:|7-commandAction|3|27-preAction
                calcular();
//GEN-LINE:|7-commandAction|4|27-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|5|7-postCommandAction
        }//GEN-END:|7-commandAction|5|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|6|
//</editor-fold>//GEN-END:|7-commandAction|6|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initialized instance of exitCommand component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Sair", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|14-getter|0|14-preInit
    /**
     * Returns an initialized instance of form component.
     *
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|14-getter|0|14-preInit
            // write pre-init user code here
            form = new Form("Conversor de Medicamentos", new Item[]{getImageLogo(), getSpacer1(), getLabelMedicamento(), getSpacer(), getMedMiligramas(), getMedMililitros(), getSpacer2(), getLabelPrescricao(), getPrescmiligramas(), getSpacer3(), getLabelResultado()});//GEN-BEGIN:|14-getter|1|14-postInit
            form.addCommand(getExitCommand());
            form.addCommand(getOkCommand());
            form.setCommandListener(this);//GEN-END:|14-getter|1|14-postInit
            // write post-init user code here
        }//GEN-BEGIN:|14-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|14-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: prescmiligramas ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initialized instance of prescmiligramas component.
     *
     * @return the initialized component instance
     */
    public TextField getPrescmiligramas() {
        if (prescmiligramas == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            prescmiligramas = new TextField("Miligramas (mg)", null, 32, TextField.DECIMAL);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return prescmiligramas;
    }
//</editor-fold>//GEN-END:|23-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initialized instance of spacer component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer() {
        if (spacer == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            spacer = new Spacer(16, 20);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return spacer;
    }
//</editor-fold>//GEN-END:|24-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: medMililitros ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initialized instance of medMililitros component.
     *
     * @return the initialized component instance
     */
    public TextField getMedMililitros() {
        if (medMililitros == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            medMililitros = new TextField("Mililitros (ml)", null, 32, TextField.DECIMAL);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return medMililitros;
    }
//</editor-fold>//GEN-END:|25-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initialized instance of okCommand component.
     *
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            okCommand = new Command("Calcular", Command.OK, 0);//GEN-LINE:|26-getter|1|26-postInit
            // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return okCommand;
    }
//</editor-fold>//GEN-END:|26-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: imageLogo ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initialized instance of imageLogo component.
     *
     * @return the initialized component instance
     */
    public ImageItem getImageLogo() {
        if (imageLogo == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            imageLogo = new ImageItem("", getImage(), ImageItem.LAYOUT_CENTER | Item.LAYOUT_TOP | Item.LAYOUT_BOTTOM | Item.LAYOUT_VCENTER | Item.LAYOUT_SHRINK | Item.LAYOUT_VSHRINK, "<Imagem perdida>", Item.PLAIN);//GEN-BEGIN:|28-getter|1|28-postInit
            imageLogo.setPreferredSize(64, 64);//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
        }//GEN-BEGIN:|28-getter|2|
        return imageLogo;
    }
//</editor-fold>//GEN-END:|28-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer1 ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initialized instance of spacer1 component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer1() {
        if (spacer1 == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            spacer1 = new Spacer(16, 10);//GEN-LINE:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return spacer1;
    }
//</editor-fold>//GEN-END:|29-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: image ">//GEN-BEGIN:|30-getter|0|30-preInit
    /**
     * Returns an initialized instance of image component.
     *
     * @return the initialized component instance
     */
    public Image getImage() {
        if (image == null) {//GEN-END:|30-getter|0|30-preInit
            // write pre-init user code here
            try {//GEN-BEGIN:|30-getter|1|30-@java.io.IOException
                image = Image.createImage("/medicalCase.png");
            } catch (java.io.IOException e) {//GEN-END:|30-getter|1|30-@java.io.IOException
                e.printStackTrace();
            }//GEN-LINE:|30-getter|2|30-postInit
            // write post-init user code here
        }//GEN-BEGIN:|30-getter|3|
        return image;
    }
//</editor-fold>//GEN-END:|30-getter|3|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer2 ">//GEN-BEGIN:|33-getter|0|33-preInit
    /**
     * Returns an initialized instance of spacer2 component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer2() {
        if (spacer2 == null) {//GEN-END:|33-getter|0|33-preInit
            // write pre-init user code here
            spacer2 = new Spacer(16, 20);//GEN-LINE:|33-getter|1|33-postInit
            // write post-init user code here
        }//GEN-BEGIN:|33-getter|2|
        return spacer2;
    }
//</editor-fold>//GEN-END:|33-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: labelResultado ">//GEN-BEGIN:|35-getter|0|35-preInit
    /**
     * Returns an initialized instance of labelResultado component.
     *
     * @return the initialized component instance
     */
    public StringItem getLabelResultado() {
        if (labelResultado == null) {//GEN-END:|35-getter|0|35-preInit
            // write pre-init user code here
            labelResultado = new StringItem("", "", Item.PLAIN);//GEN-LINE:|35-getter|1|35-postInit
            // write post-init user code here
        }//GEN-BEGIN:|35-getter|2|
        return labelResultado;
    }
//</editor-fold>//GEN-END:|35-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: medMiligramas ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initialized instance of medMiligramas component.
     *
     * @return the initialized component instance
     */
    public TextField getMedMiligramas() {
        if (medMiligramas == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            medMiligramas = new TextField("Miligramas (mg)", null, 32, TextField.DECIMAL);//GEN-LINE:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return medMiligramas;
    }
//</editor-fold>//GEN-END:|36-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: labelMedicamento ">//GEN-BEGIN:|37-getter|0|37-preInit
    /**
     * Returns an initialized instance of labelMedicamento component.
     *
     * @return the initialized component instance
     */
    public StringItem getLabelMedicamento() {
        if (labelMedicamento == null) {//GEN-END:|37-getter|0|37-preInit
            // write pre-init user code here
            labelMedicamento = new StringItem("MEDICAMENTO", null);//GEN-LINE:|37-getter|1|37-postInit
            // write post-init user code here
        }//GEN-BEGIN:|37-getter|2|
        return labelMedicamento;
    }
//</editor-fold>//GEN-END:|37-getter|2|



//<editor-fold defaultstate="collapsed" desc=" Generated Getter: labelPrescricao ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initialized instance of labelPrescricao component.
     *
     * @return the initialized component instance
     */
    public StringItem getLabelPrescricao() {
        if (labelPrescricao == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            labelPrescricao = new StringItem("PRESCRI\u00C7\u00C3O", null);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return labelPrescricao;
    }
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: spacer3 ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initialized instance of spacer3 component.
     *
     * @return the initialized component instance
     */
    public Spacer getSpacer3() {
        if (spacer3 == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            spacer3 = new Spacer(16, 10);//GEN-LINE:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return spacer3;
    }
//</editor-fold>//GEN-END:|41-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }
    
    public void calcular(){
        
        double medMg = Double.parseDouble(medMiligramas.getString());
        double medMl = Double.parseDouble(medMililitros.getString());
        double medPresMg = Double.parseDouble(prescmiligramas.getString());
        
        if(medMg > 0.0 &&
           medMl > 0.0 &&
           medPresMg > 0.0){
            
            double resultado = (medMl*medPresMg)/medMg;

            labelResultado.setLabel("RESULTADO (ml): ");
            labelResultado.setText(String.valueOf(resultado));
            
        }else{
            labelResultado.setText("Todos os campos são obrigatórios e maiores que 0");
        }
        
    }
    
}
